<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/8 15:04}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\model;
use think\Db;
use think\Model;

class UserHistory extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**
     * 添加一条记录
     */
    public static function addHistory($uid = '',$gid = '')
    {
        if (empty($uid) || empty($gid)) return false;
        $where = [];
        $where['uid'] = ['eq',$uid];
        $where['gid'] = ['eq',$gid];
        $historyList = UserHistory::where($where)->find();
        if ($historyList)
        {
            UserHistory::where('id',1)->update(['create_time' => time()]);
        } else {
            UserHistory::insert(['uid' => $uid, 'gid' => $gid, 'create_time' => time()]);
        }
        return true;
    }

    public function hislist($uid, $page, $limit, $name = '')
    {
        $where = [];
        $where['hs.uid']   = ['eq',$uid];
        $where['g.status'] = ['eq', 0];
        $where['g.name']   = ['like', '%'.$name.'%'];
        $list = Db::name('user_history')
            ->alias('hs')
            ->field('hs.id,hs.gid,g.name,g.ordinaryprice,g.haveprice,g.agentprice,hs.create_time,g.main_image')
            ->join('goods g','g.id = hs.gid', 'LEFT')
            ->where($where)
            ->order('create_time desc')
            ->paginate($limit, false, ['page' => $page]);
        $data = User::goodsprice($uid,$list->items());
        foreach($data as &$val)
        {
            $val['create_time'] = date('Y-m-d',$val['create_time']);
        }

        $response = [
            'list' => $data,
            'count' => $list->total(),
        ];

        return $response;
    }


}